package com.tyjc.unique.common;

import com.tyjc.unique.bean.SSQTrendChart;
import com.fyzk.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;


public class ZouShiInfors {
	
	public static final int hong_zouShi = 0;
	public static final int lan_zouShi = 1;
	
	/**
	 * 获取走势遗漏
	 * @param chartItems
	 * @param type 0表示红球
	 * @return
	 */
	public static int[][] getZouShi(List<SSQTrendChart> chartItems, int type){
		if(null!=chartItems){
			List<List<Integer>> zouShis = new ArrayList<List<Integer>>();
			int h = chartItems.size();
			int w = 0;
			for (int i = 0; i < h; i++) {
				String[] miss;
				if (type == 0) {
					miss = chartItems.get(i).hongMiss();
				}else{
					miss = chartItems.get(i).lanMiss();
				}
				List<Integer> hh = new ArrayList<Integer>();
				
				w = miss.length;
				for (int j = 0; j < w; j++) {
					int z = 0;
					try {
						z = Integer.parseInt(miss[j]);
					} catch (Exception e) {
						LogUtil.e(e.getMessage());
					}
					hh.add(z);
				}
				zouShis.add(hh);
			}
			
			
			int[][] zst = new int[h][w];
			for (int i = 0; i < zouShis.size(); i++) {
				for (int j = 0; j < zouShis.get(i).size(); j++) {
					zst[i][j] = zouShis.get(i).get(j);
				}
			}
			return zst;
			
		}else{
			return null;
		}
		
	}

	public static List<List<Integer>> getZouShi(List<SSQTrendChart> chartItems){
		List<List<Integer>> zouShis = new ArrayList<List<Integer>>();
		if(null!=chartItems){
			int columnCount = chartItems.size();
			for (int i = 0; i < columnCount; i++) {
				List<Integer> missRow = new ArrayList<Integer>();

				// 开奖号码
				String winCode = chartItems.get(i).getWincode();
				if (winCode.contains("|")) {	// 开奖号码包括 红球 和 蓝球
					String[] winRedAndBlues = winCode.split("\\|");
					String redCodeStr = winRedAndBlues[0];
					String blueCodeStr = winRedAndBlues[1];
					if (redCodeStr.contains(",")) {	// 开奖号码红球必须用"，"分割
						String[] redCodes = redCodeStr.split(",");
						missRow.addAll(getMiss(redCodes));
					}

					if (blueCodeStr.contains(",")) {
						String[] blueCodes = blueCodeStr.split(",");
						missRow.addAll(getMiss(blueCodes));
					} else {
						missRow.addAll(getMiss(new String[]{blueCodeStr}));
					}


				} else {	// 开奖号码只有红球
					if (winCode.contains(",")) {	// 开奖号码红球必须用"，"分割
						String[] redCodes = winCode.split(",");
						missRow.addAll(getMiss(redCodes));
					}
				}

				String[] missValue = chartItems.get(i).missValue();

				missRow.addAll(getMiss(missValue));

				zouShis.add(missRow);
			}
		}
		addTongJi(zouShis);
		return zouShis;

	}

	private static void addTongJi(List<List<Integer>> missValues) {
		if (missValues == null || missValues.size() == 0) {
			return;
		}
		int[] showCounts = new int[missValues.get(0).size()];
		int[] averageMiss = new int[missValues.get(0).size()];
		int[] maxMiss = new int[missValues.get(0).size()];
		int[] maxLianMiss = new int[missValues.get(0).size()];

		int[] lian = new int[missValues.get(0).size()];

		for (int i = 0; i < missValues.size(); i++) {
			for (int j = 0; j < missValues.get(i).size(); j++) {
				if (missValues.get(i).get(j) == 0) {
					showCounts[j]++;
				}
				averageMiss[j] += missValues.get(i).get(j);
				if (missValues.get(i).get(j) > maxMiss[j]) {
					maxMiss[j] = missValues.get(i).get(j);
				}
				if (i > 0) {
					if (missValues.get(i-1).get(j) == missValues.get(i).get(j)) {
						lian[j]++;
					} else {
						if (maxLianMiss[j] < lian[j]) {
							maxLianMiss[j] = lian[j];
						}
						lian[j] = 0;
					}
				}
			}
		}


		List<Integer> showCounts_list = new ArrayList<>();
		List<Integer> averageMiss_list = new ArrayList<>();
		List<Integer> maxMiss_list = new ArrayList<>();
		List<Integer> maxLianMiss_list = new ArrayList<>();

		for (int i = 0; i < showCounts.length; i++) {
			showCounts_list.add(showCounts[i]);
			averageMiss_list.add(averageMiss[i]/missValues.size());
			maxMiss_list.add(maxMiss[i]);
			maxLianMiss_list.add(maxLianMiss[i]+1);
		}

		missValues.add(showCounts_list);	// 出现次数
		missValues.add(averageMiss_list);	// 平均遗漏
		missValues.add(maxMiss_list);		// 最大遗漏
		missValues.add(maxLianMiss_list);	// 最大连出

	}

	/**
	 * 获取期号
	 * @param chartItems
	 * @return
	 */
	public static List<String> getPhase(List<SSQTrendChart> chartItems) {
		List<String> phase = new ArrayList<>();
		for (int i = 0; i < chartItems.size(); i++) {
			phase.add(chartItems.get(i).getPhase());
		}
		if (phase.size() != 0) {
			phase.add("出现次数");
			phase.add("平均遗漏");
			phase.add("最大遗漏");
			phase.add("最大连出");
		}
		return phase;
	}

	private static List<Integer> getMiss(String[] miss) {
		List<Integer> missInteger = new ArrayList<Integer>();

		for (int j = 0; j < miss.length; j++) {
			missInteger.add(stringToInt(miss[j]));
		}
		return missInteger;
	}

	/**
	 * 字符串转int
	 * @param str
	 * @return
	 */
	private static int stringToInt(String str) {
		int z = 0;
		try {
			z = Integer.parseInt(str);
		} catch (Exception e) {
			LogUtil.e(e.getMessage());
		}
		return z;
	}

}
